package org.jeecg.modules.user.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.MemberAPI;
import org.jeecg.common.api.dto.MemberDTO;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.bicycle.order.entity.BikeVehicleOrder;
import org.jeecg.modules.bicycle.order.mapper.BikeVehicleOrderMapper;
import org.jeecg.modules.bicycle.vehicle.entity.BikeVehicle;
import org.jeecg.modules.bicycle.vehicle.mapper.BikeVehicleMapper;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.fault.entity.BikeFaultSubmit;
import org.jeecg.modules.param.fault.mapper.BikeFaultSubmitMapper;
import org.jeecg.modules.utils.BikeResult;
import org.jeecg.modules.utils.YueYueNiaoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @Description: bike_pile
* @Author: jeecg-boot
* @Date:   2021-05-19
* @Version: V1.0
*/
@Api(tags="获取用户信息")
@RestController
@RequestMapping("/app/Member")
@Slf4j
public class getUserInfoController extends JeecgController<MemberDTO, MemberAPI> {

    @Autowired
    private MemberAPI memberAPI;

    @Autowired
    private BikeVehicleOrderMapper bikeVehicleOrderMapper;

    @Autowired
    private BikeVehicleMapper bikeVehicleMapper;

    @Autowired
    private BikeFaultSubmitMapper bikeFaultSubmitMapper;

    @Value("${application.domain}")
    public String domain;

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }



    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public BikeResult getUserInfo(HttpServletRequest request){
        String accessToken = request.getHeader("X-Access-Token");
        // 解密获得username，用于和数据库进行对比
        String username = JwtUtil.getUsername(accessToken);
        MemberDTO member = memberAPI.getMember(username);
        member.setAge(YueYueNiaoUtils.getAgeByIdCard(member.getIdCard()));
        member.setHeadIcon(domain+member.getHeadIcon());
        member.setRideStatus("1");
        //QueryWrapper q = new QueryWrapper();
        //q.eq("user_id",member.getId());
        //q.eq("is_solve","0");
        //List<BikeFaultSubmit> list= bikeFaultSubmitMapper.selectList(q);
        //BikeVehicleOrder bikeVehicleOrder1 = bikeVehicleOrderMapper.selectOne(new QueryWrapper<BikeVehicleOrder>().eq("pick_user", member.getId()).eq("is_pay", "0"));
        //if (list.size()>=1 && bikeVehicleOrder1!=null) {
        //    member.setRideStatus("0");
        //}else {
        //    member.setRideStatus("1");
        //}

        // 月月鸟  2021  9 7
        if (StringUtils.isNotEmpty(member.getIdCard())){
            //判断是否实名
            member.setIsCertification(BikeConstant.y_n_yes);
        }
        BikeVehicleOrder bikeVehicleOrder= bikeVehicleOrderMapper.selectOne( new QueryWrapper<BikeVehicleOrder>()
                .eq("pick_user",member.getId())
                .eq("is_pay","0")

        );

        if (bikeVehicleOrder==null){
            return BikeResult.ok("1","获取用户信息成功",member);
        }

        if (bikeVehicleOrder.getRideStatus().equals("4")){
            return BikeResult.ok("1","获取用户信息成功",member);
        }
        BikeVehicle bikeVehicle= bikeVehicleMapper.selectById(bikeVehicleOrder.getVehicleId());
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1=df.parse(df.format(new Date()));
            Date d2=df.parse(df.format(bikeVehicleOrder.getPickTime()));
            long diff = d1.getTime() - d2.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
            if (days==0&&hours==0){
                member.setRidingTime(minutes+"分钟");
            }else if (days==0){
                member.setRidingTime(hours+"小时"+minutes+"分钟");
            }else {
                member.setRidingTime(days+"天"+hours+"小时"+minutes+"分钟");
            }
        } catch (ParseException e) {
            return BikeResult.error("0", "获取用户信息失败",null);

        }
        member.setVehicleCode(bikeVehicle.getCode());
        return BikeResult.ok("1","获取用户信息成功",member);
    }


}
